﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public class DragLineExLayout : EquipmentLayout
	{
		public DragLineExLayout(UIDocument uiDoc, Document document, DbFamilyObject familyObject, FamilySymbol selectedFamilySymbol, List<BuiltInCategory> lstFilter) : base(uiDoc, document, familyObject, selectedFamilySymbol, lstFilter)
		{
		}

		public override bool Layout()
		{
			if (!base.Layout())
			{
				return false;
			}
			this.form = new DragLineExLayoutForm(this.doc);
			this.form.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			for (;;)
			{
				try
				{
					this.startPoint = this.docUI.Selection.PickPoint("请选取起点");
					LayoutEquipmentJig layoutEquipmentJig = new LayoutEquipmentJig(this.docUI, this.form);
					this.endPoint = layoutEquipmentJig.PickEndPoint(this.startPoint, LayoutEquipmentJig.ELayoutType.eLineExt, "请选取终点");
					layoutEquipmentJig.Dispose();
					if (this.endPoint == null)
					{
						break;
					}
					if ((this.endPoint - this.startPoint).IsZeroLength())
					{
						TaskDialog.Show("盈建科提示", "选取的两点为同一点，请选取两个不同点。");
						continue;
					}
				}
				catch (Exception)
				{
					break;
				}
				List<XYZ> insertPoints = this.GetInsertPoints();
				if (insertPoints != null && insertPoints.Count > 0)
				{
					double angle = Convert.ToDouble(this.form.Angle);
					if (this.form.IsAutoAngle)
					{
						angle = this.GetLineAngle();
					}
					TransactionGroup transactionGroup = new TransactionGroup(this.doc, "TransactionGroup");
					try
					{
						transactionGroup.Start();
						Transaction transaction = null;
						try
						{
							FamilyInstance familyInstance = null;
							foreach (XYZ xyz in insertPoints)
							{
								transaction = new Transaction(this.doc, "DragLineExLayout");
								transaction.Start();
								FamilyInstance familyInstance2 = YJKRevitTools.LayFamilySymbol(this.doc, xyz, this.familySymbol, this.doc.ActiveView.GenLevel, angle);
								if (familyInstance2 == null)
								{
									throw new Exception("布置设备出错。");
								}
								base.SetParametersValue(familyInstance2, this.dbFamilyObject.Size);
								transaction.Commit();
								if (this.form.IsLink && familyInstance != null)
								{
                                    NewConnectWire.CreateWire(this.doc, familyInstance, familyInstance2, (Autodesk.Revit.DB.Electrical.WiringType)this.form.WireStyle, null);
								}
								familyInstance = familyInstance2;
							}
						}
						catch (Exception)
						{
							if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
							{
								transaction.RollBack();
							}
							break;
						}
						transactionGroup.Assimilate();
						continue;
					}
					catch (Exception)
					{
						transactionGroup.RollBack();
						continue;
					}
					break;
				}
			}
			this.form.Close();
			return true;
		}

		private List<XYZ> GetInsertPoints()
		{
			if (this.form == null)
			{
				return null;
			}
			List<XYZ> list = new List<XYZ>();
			int count = this.form.Count;
			double margin = this.form.Margin;
			double num = this.startPoint.DistanceTo(this.endPoint);
			double num2 = (Math.Abs(2.0 * margin + (double)count - 1.0) < 1E-06) ? 0.0 : (num / (2.0 * margin + (double)count - 1.0));
			for (int i = 0; i < count; i++)
			{
				XYZ item = this.startPoint + num2 * (margin + (double)i) * (this.endPoint - this.startPoint).Normalize();
				list.Add(item);
			}
			return list;
		}

		private double GetLineAngle()
		{
			return AssistFunc.RadianToAngle(XYZ.BasisX.AngleOnPlaneTo(this.endPoint - this.startPoint, XYZ.BasisZ));
		}

		private XYZ startPoint;

		private XYZ endPoint;

		private DragLineExLayoutForm form;
	}
}
